﻿using System.Collections.Generic;
using System.Xml;
using System.ComponentModel;

namespace com.huigo.Framework.ServiceUtils
{
    public class ParserXml : IListSource
    {
        private List<Dictionary<string, string>> _FieldValuePareList;

        public List<Dictionary<string, string>> FieldValuePareList
        {
            get { return _FieldValuePareList; }
            set { _FieldValuePareList = value; }
        }

        private List<object> _ModelList;

        public List<object> ModelList
        {
            get { return Parser.AssemblyTools.GetModelList(_FieldValuePareList); }
        }

        public int ColsCount
        {
            get
            {
                if (_FieldValuePareList == null || _FieldValuePareList.Count == 0)
                    return 0;
                Dictionary<string, string> fpvs = _FieldValuePareList[0];
                return fpvs.Count;
            }
        }
        public int RowsCount
        {
            get
            {
                if (_FieldValuePareList == null)
                    return 0;
                return _FieldValuePareList.Count;
            }

        }

        private string Status;

        public string status
        {
            get { return Status; }
            set { Status = value; }
        }

        public ParserXml(string xml)
        {
            _FieldValuePareList = new List<Dictionary<string, string>>();
            ConvertXmlToList(xml);
        }

        public void ConvertXmlToList(string xml)
        {
            if (string.IsNullOrEmpty(xml)) return;

            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(xml);
            XmlNode rNode = xDoc.SelectSingleNode("//Result");

            if (rNode == null || !rNode.HasChildNodes)
                return;

            if (rNode.ChildNodes[0].HasChildNodes && rNode.ChildNodes[0].ChildNodes[0].HasChildNodes)
            {
                foreach (XmlNode cNode in rNode.ChildNodes)
                {
                    Dictionary<string, string> fpv = new Dictionary<string, string>();
                    foreach (XmlNode fNode in cNode)
                    {
                        fpv.Add(fNode.Name, fNode.InnerText);
                    }
                    _FieldValuePareList.Add(fpv);
                }
            }
            else
            {
                Dictionary<string, string> fpv = new Dictionary<string, string>();
                foreach (XmlNode cNode in rNode.ChildNodes)
                {
                    fpv.Add(cNode.Name, cNode.InnerText);
                }
                _FieldValuePareList.Add(fpv);
            }

            rNode = xDoc.SelectSingleNode("//Status");
            if (rNode == null)
                return;
            status = rNode.InnerText;
        }

        public Dictionary<string, string> this[int row]
        {
            get
            {
                if (row > RowsCount)
                    return null;

                return _FieldValuePareList[row];
            }
        }

        public string this[string name, int row]
        {
            get
            {
                if (this[row].ContainsKey(name))
                    return this[row][name];
                return string.Empty;
            }
        }

        public string this[string name]
        {
            get
            {
                if (this[0].ContainsKey(name))
                    return this[0][name];
                return string.Empty;
            }
        }

        #region IListSource 成员

        public bool ContainsListCollection
        {
            get { return false; }
        }

        public System.Collections.IList GetList()
        {
            return FieldValuePareList;
        }

        #endregion
    }
}
